import * as Cesium from 'cesium/Cesium'
import MeasurementSettings from "./MeasurementSettings";
import MeasureUnits from "./MeasureUnits";
import PolygonDrawing from "./PolygonDrawing";
import DrawingMode from "./DrawingMode";
var cart2Scratch1 = new Cesium.Cartesian2, cart2Scratch2 = new Cesium.Cartesian2, p0Scratch$1 = new Cesium.Cartesian3,
    p1Scratch$3 = new Cesium.Cartesian3, p2Scratch$3 = new Cesium.Cartesian3, v0Scratch = new Cesium.Cartesian3,
    v1Scratch$2 = new Cesium.Cartesian3;

function AreaMeasurementDrawing(e) {
    (e = Cesium.defaultValue(e, Cesium.defaultValue.EMPTY_OBJECT)).polylineOptions
        = MeasurementSettings.getPolylineOptions({
        ellipsoid: e.ellipsoid
    }),
        e.pointOptions = MeasurementSettings.getPointOptions(),
        e.polygonOptions = {
            color: MeasurementSettings.color.withAlpha(.8)
        },
        PolygonDrawing.call(this, e);
    var t = e.labels;
    this._labelCollection = t,
        this._label = t.add(MeasurementSettings.getLabelOptions()),
        this._selectedUnits = e.units,
        this._selectedLocale = e.locale,
        this._area = 0;
    var i = this;
    this._removeEvent = this._scene.preRender.addEventListener(function() {
        i.updateLabel()
    })
}
AreaMeasurementDrawing.prototype = Object.create(PolygonDrawing.prototype),
    AreaMeasurementDrawing.prototype.constructor = AreaMeasurementDrawing,
    Cesium.defineProperties(AreaMeasurementDrawing.prototype, {
        area: {
            get: function() {
                return this._area
            }
        }
    }),
    AreaMeasurementDrawing.prototype.updateArea = function(e) {
        var t = Cesium.CoplanarPolygonGeometry.createGeometry(Cesium.CoplanarPolygonGeometry.fromPositions({
            positions: e,
            vertexFormat: Cesium.VertexFormat.POSITION_ONLY
        }));
        if (Cesium.defined(t)) {
            for (var i = t.attributes.position.values, r = t.indices, n = 0, a = 0; a < r.length; a += 3) {
                var o = r[a]
                    , s = r[a + 1]
                    , l = r[a + 2];
                n += triangleArea(Cesium.Cartesian3.unpack(i, 3 * o, p0Scratch$1),
                    Cesium.Cartesian3.unpack(i, 3 * s, p1Scratch$3), Cesium.Cartesian3.unpack(i, 3 * l, p2Scratch$3))
            }
            this._area = n,
                this._label.text = MeasureUnits.areaToString(n, this._selectedUnits.areaUnits, this._selectedLocale)
        } else
            this._label.text = ""
    }
    ,
    AreaMeasurementDrawing.prototype.updateLabel = function() {
        var e = this._positions;
        if (!(e.length < 2)) {
            for (var t = this._scene, i = e[0], r = Cesium.SceneTransforms.wgs84ToWindowCoordinates(t, i, cart2Scratch1),
                     n = Cesium.defined(r) ? r.y : Number.POSITIVE_INFINITY, a = 1; a < e.length; a++) {
                var o =Cesium. SceneTransforms.wgs84ToWindowCoordinates(t, e[a], cart2Scratch2);
                Cesium.defined(o) && o.y < n && (n = o.y,
                    i = e[a])
            }
            this._label.position = i
        }
    }
    ,
    AreaMeasurementDrawing.prototype.addPoint = function(e) {
        PolygonDrawing.prototype.addPoint.call(this, e),
            this.updateArea(this._positions)
    }
    ,
    AreaMeasurementDrawing.prototype.handleClick = function(e) {
        this._mode === DrawingMode.AfterDraw && this.reset(),
        Cesium.defined(PolygonDrawing.prototype.handleClick.call(this, e)) && (this._label.show = !0,
            this._polygon.show = !0,
            this._polyline.show = !0)
    }
    ,
    AreaMeasurementDrawing.prototype.handleMouseMove = function(e) {
        var t = PolygonDrawing.prototype.handleMouseMove.call(this, e);
        if (Cesium.defined(t)) {
            var i = this._positions.slice();
            i.push(t),
                this.updateArea(i)
        }
    }
    ,
    AreaMeasurementDrawing.prototype.reset = function() {
        this._label.show = !1,
            this._label.text = "",
            this._positions = [],
            this._polyline.positions = [],
            this._polygon.positions = [],
            this._polyline.show = !1,
            this._polygon.show = !1,
            this._area = 0;
        for (var e = this._points, t = this._pointCollection, i = 0; i < e.length; i++)
            t.remove(e[i]);
        e.length = 0,
            this._mode = DrawingMode.BeforeDraw,
            this._lastClickPosition.x = Number.POSITIVE_INFINITY,
            this._lastClickPosition.y = Number.POSITIVE_INFINITY
    }
    ,
    AreaMeasurementDrawing.prototype.destroy = function() {
        this._removeEvent(),
            this._labelCollection.remove(this._label),
            PolygonDrawing.prototype.destroy.call(this)
    };

function triangleArea(e, t, i) {
    var r = Cesium.Cartesian3.subtract(e, t, v0Scratch), n = Cesium.Cartesian3.subtract(i, t, v1Scratch$2),
        a = Cesium.Cartesian3.cross(r, n, r);
    return .5 * Cesium.Cartesian3.magnitude(a)
}
export default AreaMeasurementDrawing;

